package com.second.app.thread.线程基础.day02;

/**
 * @Author GYZ
 * @Date 2023/11/17
 * <pre>
 *    案例1：
 *        Thread.currentThread(),主要是返回正在被哪个线程调用的信息。
 *        构造方法执行是main线程，而run方法执行，就是Thread-0线程。
 *    案例2：
 *        获取线程名称:设置线程对象名称为A,在构造方法和run方法执行时候，线程名称获取都是A
 *        判断线程是否isAlive：true(存活)；false(未存活)
 *        this和Thread.currentThread()区别
 *
 * </pre>
 */
public class Main {

    public static void main(String[] args) {
        class MThread extends Thread{
            public MThread(){
                System.out.println("构造方法的打印："+Thread.currentThread().getName());
            }
            @Override
            public void run() {
                System.out.println("run方法的打印："+Thread.currentThread().getName());
            }
        }
//        MThread mThread = new MThread();
//        mThread.start();

        class CountOperate extends Thread{
            public CountOperate(){
                System.out.println("CountOperate---begin");
                System.out.println("Thread.currentThread.getName()="+Thread.currentThread().getName());//main
                System.out.println("Thread.currentThread.isAlive()="+Thread.currentThread().isAlive());//true
                System.out.println("this.getName()="+this.getName());//A
                System.out.println("this.isAlive()="+this.isAlive());//false
                System.out.println("CountOperate---end");
            }

            @Override
            public void run() {
                System.out.println("run---begin");
                System.out.println("Thread.currentThread.getName()="+Thread.currentThread().getName());//thread-0
                System.out.println("Thread.currentThread.isAlive()="+Thread.currentThread().isAlive());//true
                System.out.println("this.getName()="+this.getName());//A
                System.out.println("this.isAlive()="+this.isAlive());//false
                System.out.println("run---end");
            }
        }
        CountOperate countOperate = new CountOperate();
        Thread t1 = new Thread(countOperate);
        t1.setName("A");
        System.out.println("main begin  t1 isAlive="+t1.isAlive()); //false
        t1.start();
        System.out.println("main end  t1 isAlive="+t1.isAlive());//true
    }
}
